package com.miyu.client1.test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

public class javaDate01 {
    public static void main(String[] args) {

        System.out.println(">>>>>>>>>>>>>>>>>LocalDate是日期处理类<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
// 获取当前日期
        LocalDate now = LocalDate.now();

        System.out.println("获取当前日期: "+now);
// 设置日期
        LocalDate localDate = LocalDate.of(2019, 10, 11);

        System.out.println("设置日期: "+localDate);
// 获取年
        int year = localDate.getYear();     //结果：2019
        int year1 = localDate.get(ChronoField.YEAR); //结果：2019
        System.out.println("year: "+year);
        System.out.println("year1: "+year1);
// 获取月
        Month month = localDate.getMonth();   // 结果：SEPTEMBER
        int month1 = localDate.get(ChronoField.MONTH_OF_YEAR); //结果：9

        System.out.println("month: "+month);
        System.out.println("month1: "+month1);
// 获取日
        int day = localDate.getDayOfMonth();   //结果：10
        int day1 = localDate.get(ChronoField.DAY_OF_MONTH); // 结果：10

        System.out.println("day: "+day);
        System.out.println("day1: "+day1);
// 获取星期
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();   //结果：TUESDAY
        int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK); //结果：2
        System.out.println("dayOfWeek: "+dayOfWeek);
        System.out.println("dayOfWeek1 : "+dayOfWeek1);

        System.out.println(">>>>>>>>>>>>>>>>>LocalTime是时间处理类<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        // 获取当前时间
        LocalTime nowTime = LocalTime.now();
        System.out.println("当前时间： "+nowTime);
// 设置时间
        LocalTime localTime = LocalTime.of(13, 51, 10);

        System.out.println("设置时间： "+localTime);
//获取小时
        int hour = localTime.getHour();    // 结果：13
        int hour1 = localTime.get(ChronoField.HOUR_OF_DAY); // 结果：13

        System.out.println("hour： "+hour);
        System.out.println("hour1： "+hour1);
//获取分
        int minute = localTime.getMinute();  // 结果：51
        int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR); // 结果：51

        System.out.println("minute： "+minute);
        System.out.println("minute1： "+minute1);
//获取秒
        int second = localTime.getSecond();   // 结果：10
        int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE); // 结果：10

        System.out.println("second： "+second);
        System.out.println("second1： "+second1);

        System.out.println(">>>>>>>>>>>>>>>>>LocalDateTime可以设置年月日时分秒<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        // 获取当前日期时间
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println("获取当前日期时间： "+localDateTime);

// 设置日期
        LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);
        LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
        LocalDateTime localDateTime3 = localDate.atTime(localTime);
        LocalDateTime localDateTime4 = localTime.atDate(localDate);

        System.out.println("localDateTime1： "+localDateTime1);
        System.out.println("localDateTime2： "+localDateTime2);
        System.out.println("localDateTime3： "+localDateTime3);
        System.out.println("localDateTime4： "+localDateTime4);

// 获取LocalDate
        LocalDate localDate2 = localDateTime.toLocalDate();

        System.out.println("localDate2： "+localDate2);

// 获取LocalTime
        LocalTime localTime2 = localDateTime.toLocalTime();

        System.out.println("localTime2： "+localTime2);


        // 创建日期：2019-09-10 14:46:56
//        LocalDateTime localDateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);
//增加一年
        localDateTime = localDateTime.plusYears(1);  //结果： 2020-09-10 14:46:56
        localDateTime = localDateTime.plus(1, ChronoUnit.YEARS); //结果： 2021-09-10 14:46:56

        System.out.println("localDateTime： "+localDateTime);
        System.out.println("localDateTime： "+localDateTime);

//减少一个月
        localDateTime = localDateTime.minusMonths(1);  //结果： 2021-08-10 14:46:56
        localDateTime = localDateTime.minus(1, ChronoUnit.MONTHS); //结果： 2021-07-10 14:46:56

        System.out.println("localDateTime： "+localDateTime);
        System.out.println("localDateTime： "+localDateTime);


   //     LocalDate localDate = LocalDate.of(2019, 9, 10);
        String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
        String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);


        System.out.println("s1： "+s1);
        System.out.println("s2： "+s2);

//自定义格式化
        DateTimeFormatter dateTimeFormatter =   DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String s3 = localDate.format(dateTimeFormatter);

        System.out.println("s3： "+s3);
    }
}
